package com.leapp.sk.viewdelegate.personal

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.ListView
import android.widget.PopupWindow
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.NoScrollGridView
import com.example.mvp.view.PopupWindowUtil
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R
import com.leapp.sk.bean.ChoseClassEntity
import com.leapp.sk.bean.ClassIndexRand
import com.leapp.sk.ui.activity.task.CompleteTaskActivity
import com.leapp.sk.ui.adapter.ClassIndexRankAdapter
import com.leapp.sk.ui.adapter.TopChoseClassAdapter

/**
 * 班级指数
 * @author  chaohaipeng  on 2018/8/14.
 */
class ClassIndexDelegate:BaseAppDelegate() {
    private var popupWindow: PopupWindow? = null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_class_index
    }


     fun initAdapter(context: Context, mlist: List<ClassIndexRand>){
         val gv_aci_index_rank = getView<NoScrollGridView>(R.id.gv_aci_index_rank)
         val classIndexRankAdapter = ClassIndexRankAdapter(context, mlist)
         gv_aci_index_rank.adapter=classIndexRankAdapter
     }

    /**
     * 初始化班级
     */
    fun  initClassData(context: Context,mlist: List<ChoseClassEntity>){
        popupWindow = PopupWindowUtil.getInstance().initView(initFootView(context,mlist), LKScreenUtil.getScreenWidth(), LinearLayout.LayoutParams.WRAP_CONTENT)
    }
    private fun initFootView(context: Context,mlist: List<ChoseClassEntity>): View {
        val view  = LayoutInflater.from(context).inflate(R.layout.layout_join_class_chose_popuwindow, null)
        val findViewById = view.findViewById<ListView>(R.id.lv_ljccp_list)
        val topChoseClassAdapter = TopChoseClassAdapter(context, mlist)
        findViewById.adapter=topChoseClassAdapter
        findViewById.setOnItemClickListener{adapterView, view, i, l ->
            popupWindow?.dismiss()

        }


        return view
    }

    /**
     * 设置标题
     */
    fun setTitle(title:String?){
        val topBar= getView<SKTopBar>(R.id.jtb_aci_index)
        topBar.inVisibleLine()
        if (!TextUtils.isEmpty(title)){
            topBar?.setTitle(title)
        }
        setEvent(topBar)
    }
    /**
     * 设置设置事件
     */
    private  fun setEvent(topBar: SKTopBar){
        //消失监听听
        popupWindow?.setOnDismissListener(object:PopupWindow.OnDismissListener{
            override fun onDismiss() {
                topBar.setImageSelector(false)
            }

        })
        topBar.inVisibleBottom()
        topBar?.setEventBottomInterface(object : SKTopBar.EventBottomInterface{
            override fun openOnClick() {
                val showing = popupWindow?.isShowing
                if (showing!!){
                    popupWindow?.dismiss()
                }else{
                    topBar.setImageSelector(true)
                    popupWindow?.showAsDropDown(topBar,0,0)
                }
            }

        })

        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<AppCompatActivity>().finish()
            }
            override fun rightOnClick() {
                val intent= Intent(getActivity<AppCompatActivity>(), CompleteTaskActivity::class.java)
                getActivity<AppCompatActivity>().startActivity(intent)
            }

        })
    }
}